import rpm
from pkgr.tests.pkgrtestcase import PkgrTestCase
import os
import logging
import solv

logger = logging.getLogger('pkgr.test.test_pkgr')
class Test_Pkg_Basic(PkgrTestCase):
    def test_install(self):
        """Installing a package and its deps."""
        pkgr = self.pkgr
        
        pool = pkgr.get_solv_pool()
        jobs = pkgr.names_to_jobs('install', ['a'])
        pkgr.set_jobs(jobs)
        
        for job in jobs:
            job.how |= solv.Job.SOLVER_INSTALL
            
        solver = pool.Solver()
        solver.set_flag(solv.Solver.SOLVER_FLAG_SPLITPROVIDES, 1);
        
        problems = solver.solve(jobs)
        
        self.assertTrue(len(problems) == 0, "Solving should not return problems")
        
        # no problems, show transaction
        trans = solver.transaction()
        # gc/clear refcounts to the solver
        del solver
        
        self.assertTrue(not trans.isempty())
        
        rpmrunproblems = pkgr.run_transaction(trans)
        
        self.assertTrue(not rpmrunproblems, "Running rpm transaction shouldn't have problems.")
        
        if rpmrunproblems:
            print rpmrunproblems
            
        self.assertTrue(os.path.exists(os.path.join(self.installroot, 'a')))
        self.assertTrue(os.path.exists(os.path.join(self.installroot, 'b')))
        self.assertTrue(os.path.exists(os.path.join(self.installroot, 'c')))
        
        ts = rpm.TransactionSet(self.installroot)
        pkgs = ts.dbMatch() # RPM package list
        pkgs.pattern( 'name', rpm.RPMMIRE_DEFAULT, 'a' )
        self.assertTrue(len(list(pkgs)) >= 1)
        
            
        
        self.dump_install_root()
    #def test_remove(self):
    #    ''' Test package removal '''
    #    pkgr = self.pkgr
    #    
    #    packages = self.pkgr.find_package('a')
    #    logger.debug('Packages matching a: %s', packages)
    #    package = self.pkgr.get_latest_package(packages)
    #    
    #    pkgr.install_package(package)
    #    pkgr.resolve_deps()
    #    pkgr.run()
    #    
    #    self.dump_install_root()
    #    
    #    pkgr.remove('a')
    #    pkgr.resolve_deps()
    #    pkgr.run()
    #    
    #    self.assertTrue(not os.path.exists(os.path.join(self.installroot, 'a')))
    #    #pkg.remove()
    #    self.dump_install_root()
     
